<template>
    <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
    <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>权限管理</el-breadcrumb-item>
    <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>  
    <!-- 卡片视图 -->
    <el-card>
      <!-- 添加角色按钮区域 -->
      <el-row>
        <el-col>
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <el-table :data="rolelist" border stripe>
        <!-- 展开行 -->
        <el-table-column type="expand">
            <template slot-scope="scope">
                <el-row :class="['bdbottom','vcenter',i1==0?'bdtop':'']" v-for="item1,i1 in scope.row.children" :key="item1.id">
                    <!-- 显示一级权限 -->
                    <el-col :span="5">
                        <el-tag @close="removeRightById(scope.row,item1.id)" closable>{{item1.authName }}</el-tag> 
                    </el-col>
                    <!-- 显示2,3级权限 -->
                    <el-col :span="19">
                       <el-row :class="[i2==0?'':'bdtop','vcenter']" v-for="item2,i2 in item1.children" :key="item2.id">
                          <el-col :span="6">
                            <el-tag @close="removeRightById(scope.row,item2.id)" type="success" closable>{{ item2.authName }}</el-tag>
                          </el-col>
                          <el-col :span="18">
                            <el-tag @close="removeRightById(scope.row,item3.id)" :key="item3.id" v-for="item3 in item2.children" type="warning" closable>
                                {{ item3.authName }}
                            </el-tag>
                          </el-col>
                       </el-row>
                    </el-col>
                </el-row>
            </template>
        </el-table-column>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"></el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
        <el-table-column width="300px" label="角色操作" >
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
            <el-button @click="showSetRights(scope.row)" size="mini" type="warning" icon="el-icon-setting">分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 权限对话框 -->
      <el-dialog
      title="分配权限"
      :visible.sync="rightsDialog"
      width="30%"
      :before-close="handleClose">
      <!-- 放个树 -->
      <el-tree
        ref="treeRef"
        default-expand-all
        :data="rightslist"
        show-checkbox
        node-key="id"
        :default-checked-keys="defKeys"
        :props="treeProps">
      </el-tree>
      <!--  -->
      <span slot="footer" class="dialog-footer">
         <el-button @click="handleClose">取 消</el-button>
         <el-button @click="setRights" type="primary">确 定</el-button>
      </span>
      </el-dialog>
    </el-card>

    </div>
 </template>
 
 <script>
 export default {
    data(){
        return {
            // 所有角色列表数据
            rolelist: [],
            rightsDialog: false,
            rightslist: [],  //树数据
            treeProps: {
              label: 'authName',
              children: 'children'
            },
            // 默认选中的节点Id值数组
            defKeys: [],
            roleId:''  //点击的角色
        }
    },
    created(){
        this.getRoleList()
    },
    methods:{
        async getRoleList(){
            let ret = await this.http.get('roles')
            console.log( ret );
            this.rolelist = ret.data
        },
        //通过id 删除权限
        async removeRightById(role,rightId){
            console.log(role.id+'角色', rightId);
            let path = `roles/${role.id}/rights/${rightId}`
            let ret = await this.http.delete( path)
            console.log( ret );
            //界面更新
            role.children = ret.data
        },
        //显示设置权限对话框
        async showSetRights(role){
          console.log( role );
          this.roleId = role.id;
          this.rightsDialog = true
          //获取所有权限
          let ret = await this.http.get('rights/tree')
          this.rightslist = ret.data
          console.log( ret);
          this.getLeafKeys(role ,this.defKeys );
          console.log('三级点', this.defKeys);
        },
        //对话框关闭前...
        handleClose(){
           console.log('对话框关闭前...');
           this.defKeys = []
           this.rightsDialog = false
        },
        // 递归查找三级节点 node 
        getLeafKeys(node , arr){
          //如果没有孩子，就是三级节点
          if (!node.children) {
            return arr.push(node.id)
          }
          node.children.forEach( item => this.getLeafKeys(item,arr))
        },
        //完成 角色的授权
        async setRights(){
           let a = this.$refs.treeRef.getCheckedKeys()
           let b = this.$refs.treeRef.getHalfCheckedKeys()
           let c = [...a,...b]  //合并
           let strId = c.join(',')
           let ret =await this.http.post(`roles/${this.roleId}/rights`,{ rids: strId})
           if (ret.meta.status !== 200) {
              return this.$message.error( ret.meta.msg)
           }
           this.$message.success( ret.meta.msg)
           this.getRoleList()  //刷新
           this.rightsDialog = false
        }
    }
 }
 </script>
 
 <style lang="less" scoped>
.el-tag {  margin: 7px; }
.bdtop { border-top: 1px solid #eee; }
.bdbottom { border-bottom: 1px solid #eee;}
.vcenter {
  display: flex;
  align-items: center;
}
 </style>